package mytest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by guoys on 2017/2/15.
 */
public final class DateUtils {

    private static final String DATETIME_PATTERN = "yyyyMMddHHmmss";

    private static final String DATETIME_PATTERN02 = "yyyyMMddHHmmssSSS";

    public static String getCurrentTimeAsStr() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return format.format(date);
    }

    public static String getCurrentTime() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        return format.format(date);
    }

    public static String getTimeStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        return format.format(date);
    }

    public static String formatTimeStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATETIME_PATTERN02);
        return format.format(date);
    }

    public static String getCurrentTime(String formatStr) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(date);
    }


    /**
     * 获取yyyy-MM-dd格式的日期字符串
     */
    public static String getFormatDate() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 获取yyyyMMdd格式的日期字符串
     */
    public static String getSimpleDate() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(date);
    }

    /**
     * 获取HH:mm:ss格式的时间字符串
     */
    public static String getFormatTime() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        return format.format(date);
    }

    /**
     * 获取一个月份内所有的天数
     *
     * @param date 格式为 yyyyMM
     * @return 天数的集合， 格式为 yyyyMMdd
     */
    public static List<String> getAllDaysByMonth(String date) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

        Date parse = sdf.parse(date);

        List<String> results = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();

        cal.setTime(parse);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Calendar start = (Calendar) cal.clone();

        cal.add(Calendar.MONTH, 1);

        Calendar end = (Calendar) cal.clone();
        long days = (end.getTimeInMillis() - start.getTimeInMillis()) / 86400000;
        for (long i = 1; i <= days; i++) {
            if (i < 10) {
                results.add(String.format("%s0%s", date, Long.toString(i)));
            } else {
                results.add(String.format("%s%s", date, Long.toString(i)));
            }
        }
        return results;
    }

    /**
     * 向前12个月
     *
     * @return 格式为 yyyyMMdd
     */
    public static String getLastYear() {

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1);//取当前日期的前一天.
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(cal.getTime());
    }

    public static List<String> getLastYearAllMonth() {
        List<String> result = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1);//取当前日期的前一天.
        cal.add(Calendar.MONTH, 2);
        cal.set(Calendar.DAY_OF_MONTH, 1);

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);

        for (int i = 0; i < 12; i++) {
            String monthStr = month < 10 ? ("0" + month) : Integer.toString(month);
            String yearStr = Integer.toString(year);
            result.add(String.format("%s%s", yearStr, monthStr));
            if (month == 12) {
                year += 1;
                month = 1;
            } else {
                month += 1;
            }
        }

        return result;
    }

    public static Date getThousandYearAfter() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, 1000);//取当前日期的前一天.
        return cal.getTime();
    }

    public static Date getThousandYearBefore() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1000);//取当前日期的前一天.
        return cal.getTime();
    }


    public static void main(String[] args) {

        System.out.println(getLastYear());
    }

    public static String getCurrentDateTimeByPattern(String s) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(s);
        return sdf.format(date);
    }

    public static String getCurrentDateTimeMills() {
        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
    }

    /**
     * 格式化yyyyMMddHHmmssSSS格式的字符串为Date
     *
     * @param str yyyyMMddHHmmssSSS格式的字符串
     * @Author Jasper
     */
    public static Date dateTimeMills2Date(String str) {
        try {
            return new SimpleDateFormat("yyyyMMddHHmmssSSS").parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
